package com.test.daily.leetcode.y2022.m05.day0516.v03;

/**
 * @author Tom on 2022/5/17
 */
public class Solution {
    public static class Heap {
        int limit;
        int heapSize;
        int[] arr;

        public Heap(int limit) {
            this.limit = limit;
            this.heapSize = 0;
            this.arr = new int[this.limit];
        }

        public static void swap(int[] arr, int i, int j) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }

        public static void heapInsert(int[] arr, int index) {
            while (arr[index] < arr[(index - 1) / 2]) {
                swap(arr, index, (index - 1) / 2);
                index = (index - 1) / 2;
            }
        }

        public static void heapify(int[] arr, int index, int heapSize) {
            int left = index * 2 + 1;
            while(left < heapSize){
                int largest = (left + 1) < heapSize && arr[left + 1] > arr[left] ? left + 1 : left;
                largest = arr[index] > largest ? index : largest;
                if(largest == index){
                    break;
                }
                swap(arr, index, largest);
                index = largest;
                left = index * 2 + 1;
            }


        }
    }
}
